﻿//https://leetcode.cn/problems/valid-parentheses/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    bool isValid(string s)
    {
        stack<char> st;

        for (auto& e : s)
        {
            if (e == '(' || e == '{' || e == '[')
            {
                //左括号入栈
                st.push(e);
            }
            else
            {
                //右括号匹配
                if (st.empty())
                {
                    return false;
                }
                //弹出栈顶元素
                char ch = st.top();
                st.pop();
                if ((e == ')' && ch != '(') || (e == '}' && ch != '{') || (e == ']' && ch != '['))
                    return false;

            }
        }
        if (!st.empty()) return false;
        return true;
    }
};
